<?php

namespace lc\wechat\qywx;

use lc\helpers\Curl;

class Message extends Oauth
{
    /**
     * @var string 请求地址
     */
    private $baseUrl = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=';

    /**
     * @var 消息类型：卡片
     */
    const MSG_TYPE_TEXT_CARD = 'textcard';

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @desc    发送消息
     * @param   array $arguments
     */
    public function send(array $arguments)
    {
        switch ($arguments['msgtype']) {
            case 'textcard':
                self::textcard($arguments);
                break;
            default:
                break;
        }
    }

    /**
     * @param array $args
     */
    private function textcard(array $args)
    {
        $data = [
            'touser'        => $args['touser'] ?? '',
            'toparty'       => $args['toparty'] ?? '',
            'totag'         => $args['totag'] ?? '',
            'msgtype'       => self::MSG_TYPE_TEXT_CARD,
            'agentid'       => $this->getAgentId(),
            'textcard'      => [
                'title'         => $args['title'],
                'description'   => $args['description'],
                'url'           => $args['url'],
                'btntxt'        => $args['btntxt'] ?? '详情'
            ],
        ];
        $accessToken = $this->accessToken();
        Curl::requestCurl($this->baseUrl . $accessToken, 'POST', ['Content-Type: application/json'], json_encode($data));
    }
}